﻿using IISWebManager.Domain.Abstract;
using IISWebManager.Domain.Entities;
using Microsoft.Web.Administration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IISWebManager.Domain.Concrete
{
    public class WebRepository : IWebRepository
    {
        public List<SiteParameter> GetSites()
        {
            List<SiteParameter> sites = new List<SiteParameter>();

            ServerManager serverManager = new ServerManager();
            foreach (Site site in serverManager.Sites)
                foreach (Application app in site.Applications)
                    foreach (VirtualDirectory dir in app.VirtualDirectories)
                    {
                        if (app.Path.Length > 1)
                            sites.Add(new SiteParameter() { Name = string.Format("{0} / {1}", site.Name, app.Path.Replace("/", string.Empty)), Pool = app.ApplicationPoolName, Path = dir.PhysicalPath });
                        else
                            sites.Add(new SiteParameter() { Name = site.Name, Pool = app.ApplicationPoolName, Path = dir.PhysicalPath });
                    }

            return sites;
        }
    }
}
